const request = require('../../../../../utils/request')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        //控制显示
        selected: false,

        rechargeModalShow: false,

        step: 1,
        password: '',
        newPassword: '',
        confirmPassword: '',

        code: '', //卡密
        paypwd: '', //支付密码
    },
    onShow() {
        getApp().getUserInfo()
    },
    inputWrite(e) {
        this.setData({
            [e.currentTarget.dataset.name]: e.detail.value
        })
    },
    //  礼品卡规则
    toRules() {
        wx.navigateTo({
            url: '/pages/mine/pages/my/qualification/index?type=4',
        })
    },
    scanCode() {
        // 允许从相机和相册扫码
        wx.scanCode({
            success(res) {
            }
        })
    },
    // 确认充值
    sure() {
        let pay_password = wx.getStorageSync('userInfo').pay_password
        if (pay_password) {
            if (!this.data.code) {
                return getApp().toast('请输入卡密')
            }
            if (!this.data.selected) {
                return getApp().toast('请阅读并同意礼品卡规则')
            }
            this.setData({
                rechargeModalShow: true
            });
        } else {
            // 直接充值
            this.payApi()
        }
    },
    // 支付密码验证成功
    pwdcheck_success(e) {
        this.setData({
            paypwd: e.detail
        })
        this.payApi()
    },
    // 支付密码验证失败
    pwdcheck_fail() {
        this.setData({
            rechargeModalShow: false
        })
    },
    payApi() {
        let _this = this
        request._post('/api?method=user.userProduction', {
            paypwd: this.data.paypwd ? this.data.paypwd : '',
            code: this.data.code
        },'noload').then(res => {
            _this.setData({
                rechargeModalShow: false
            })
        }).catch(res => {
            setTimeout(function () {
                _this.setData({
                    rechargeModalShow: false,
                })
            }, 1000)
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },
    /**
     * 控制显示按钮
     */
    show() {
        this.setData({
            selected: !this.data.selected
        })
    },
})